package cn.com.cetccst.appstore.ui.fragment;

import static cn.com.cetccst.appstore.ui.activity.viewmodel.AppStoreViewModel.TYPE_APP_CATEGORY;
import static cn.com.cetccst.appstore.ui.activity.viewmodel.AppStoreViewModel.TYPE_APP_LIST;
import static cn.com.cetccst.tools.device.Device.getScreenWidth;

import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback;

import java.util.ArrayList;
import java.util.List;

import cn.com.cetccst.appstore.BR;
import cn.com.cetccst.appstore.R;
import cn.com.cetccst.appstore.databinding.FragmentStoreBinding;
import cn.com.cetccst.appstore.ui.activity.viewmodel.AppStoreViewModel;
import cn.com.cetccst.appstore.ui.fragment.viewmodel.StoreViewModel;
import cn.com.cetccst.uicomponents.binding.app.DataBindingConfig;

public class StoreFragment extends BaseFragment<FragmentStoreBinding> {

    private AppStoreViewModel appStoreViewModel;
    private StoreViewModel viewModel;
    private List<Fragment> fragments;

    @Override
    protected void initViewModel() {
        appStoreViewModel = getActivityScopeViewModel(AppStoreViewModel.class);
        viewModel = getFragmentScopeViewModel(StoreViewModel.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        fragments = new ArrayList<>();
        fragments.add(new AppListFragment());
        fragments.add(new CategoryListFragment());

        return new DataBindingConfig(R.layout.fragment_store, BR.viewModel, viewModel)
                .addBindingParam(BR.adapter, new StoreFragmentAdapter(getChildFragmentManager(), getLifecycle(), fragments))
                .addBindingParam(BR.click, new ClickProxy())
                .addBindingParam(BR.pageChangeListener, new TabOnPageChangeListener());
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getBinding().tabIndicator.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int width = getBinding().tabIndicator.getMeasuredWidth();
                int screenWidth = getScreenWidth(getActivity());
                getBinding().tabIndicator.setX((screenWidth >> 2) - (width >> 1));
                getBinding().tabIndicator.getViewTreeObserver().removeOnPreDrawListener(this);
                return false;
            }
        });
    }

    public class TabOnPageChangeListener extends OnPageChangeCallback {

        private int screenWidth;

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            viewModel.tabIndicatorLeftMargin.set((int) ((positionOffset + position) * screenWidth / fragments.size()));
        }

        @Override
        public void onPageSelected(int position) {
            if (position != viewModel.selectedPosition.getValue()) {
                setCurrentPostion(position);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            super.onPageScrollStateChanged(state);
        }

        public TabOnPageChangeListener() {
            screenWidth = getScreenWidth(getActivity());
        }
    }

    private void setCurrentPostion(int position) {
        viewModel.selectedPosition.setValue(position);
        switch (position) {
            case 0:
                appStoreViewModel.pageType.setValue(TYPE_APP_LIST);
                break;
            case 1:
                appStoreViewModel.pageType.setValue(TYPE_APP_CATEGORY);
                break;
        }
    }

    public static class StoreFragmentAdapter extends FragmentStateAdapter {
        private final List<Fragment> fragments;

        public StoreFragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> fragments) {
            super(fragmentManager, lifecycle);
            this.fragments = fragments;
        }
        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragments.get(position);
        }

        @Override
        public int getItemCount() {
            return fragments.size();
        }

    }

    public class ClickProxy {

        public void selectPosition(int position) {
            setCurrentPostion(position);
        }
    }

    @BindingAdapter(value = {"typeSelected"})
    public static void viewSelected(final TextView textView, boolean selected) {
        textView.setSelected(selected);
    }


    @BindingAdapter(value = {"leftMargin"})
    public static void leftMargin(View view, int leftMargin) {
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
        lp.leftMargin = leftMargin;
        view.setLayoutParams(lp);
    }
}
